// 设置页面-开关的寄存器地址
const settingSwitchRegisterAddress = 0x0101;
// 本地存储对象的key
const settingSwitchObjectKey = "settingSwitchObject"

// 初始化开关配置
function initSettingSwitch(settingSwitchObject) {
    // 获取本地缓存的开关数据
    let settingSwitchLocalObject = serialLocalStorage.get(settingSwitchObjectKey)
    if (settingSwitchLocalObject == null) {
        serialLocalStorage.put(settingSwitchObjectKey, settingSwitchObject);
        console.log("[初始化设置页面的开关数据（无缓存）]"+ JSON.stringify(settingSwitchObject));
    }
}

// 更新本地开关的设置值
function getLocalTemperatureSwitch(settingSwitchKey) {
    // 获取本地缓存的温度数据
    let settingSwitchLocalObject = serialLocalStorage.get(settingSwitchObjectKey)

    // 返回指温度的值
    let switchStatus = settingSwitchLocalObject[settingSwitchKey];

    console.log("查询本地开关的设置值[" + settingSwitchKey + "]的值：" + switchStatus + "成功");
    return switchStatus;
}

// 更新开关状态
function updateSwitchStatus(switchKey, switchState) {
    // 获取本地缓存的开关数据
    let settingSwitchObject = serialLocalStorage.get(settingSwitchObjectKey)
    console.log("更新本地开关的设置值[" + switchKey + "]的值：" + switchState);
    // 设置开关状态
    settingSwitchObject[switchKey] = switchState
    // 更新本地存储的开关数据
    serialLocalStorage.put(settingSwitchObjectKey, settingSwitchObject)
    console.log("更新设置页面的开关[" + switchKey + "]的状态：" + switchState + "成功");
    return true;
}

/**
 * setPushRodSwitch 设置推杆开关
 * @param state
 * @param registerAddress
 * @param bitPosition
 * @return {boolean}
 */
function setPushRodSwitch(state = true, bitPosition = 0, registerAddress = 0x0101) {
    // 发送命令
    setBitPositionValueReq(registerAddress, bitPosition, state)

    // 更新开关状态
    updateSwitchStatus("push_rod_switch", state);
    return true;
}

/**
 * setFrontCabinTemperatureSwitch 前舱加热开关
 * @param state
 * @param registerAddress
 * @param bitPosition
 * @return {boolean}
 */
function setFrontCabinTemperatureSwitch(state = true, bitPosition = 1, registerAddress = 0x0101) {
    // 发送命令
    setBitPositionValueReq(registerAddress, bitPosition, state)
    // 更新开关状态
    updateSwitchStatus("front_cabin_temperature_switch", state);
    return true;
}

/**
 * setRearCabinTemperatureSwitch 设置后仓加热开关
 * @param state
 * @param registerAddress
 * @param bitPosition
 * @return {boolean}
 */
function setRearCabinTemperatureSwitch(state = true, bitPosition = 2, registerAddress = 0x0101) {
    console.log("setRearCabinTemperatureSwitch"+state);
    // 发送命令
    setBitPositionValueReq(registerAddress, bitPosition, state)

    // 更新开关状态
    updateSwitchStatus("rear_cabin_temperature_switch", state);
    return true;
}

/**
 * setRockTemperatureSwitch 设置岩盘加热开关
 * @param state
 * @param bitPosition
 * @param registerAddress
 * @return {boolean}
 */
function setRockTemperatureSwitch(state = true, bitPosition = 3, registerAddress = 0x0101) {
    // 发送命令
    setBitPositionValueReq(registerAddress, bitPosition, state)

    // 更新开关状态
    updateSwitchStatus("rock_temperature_switch", state);
    return true;
}

/**
 * setUVCTemperatureSwitch 设置UVC杀菌灯开关
 * @param state
 * @param bitPosition
 * @param registerAddress
 * @return {boolean}
 */
function setUVCSwitch(state = true, bitPosition = 4, registerAddress = 0x0101) {
    // 发送命令
    setBitPositionValueReq(registerAddress, bitPosition, state)

    // 更新开关状态
    updateSwitchStatus("uvc_switch", state);

    // 更新缓存数据

    return true;
}


/**
 * setNegativeIonSwitch 设置负离子产生器开关
 * @param state
 * @param bitPosition
 * @param registerAddress
 * @return {boolean}
 */
function setNegativeIonSwitch(state = true, bitPosition = 5, registerAddress = 0x0101) {
    // 发送命令
    setBitPositionValueReq(registerAddress, bitPosition, state)

    // 更新开关状态
    updateSwitchStatus("negative_ion_switch", state);
    return true;
}
